package com.atguigu.flink.window.agg;

import com.atguigu.flink.pojo.WaterSensor;
import com.atguigu.flink.util.MyUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * Created by Smexy on 2022/11/22
 *
 *     每10s，求所有传感器的平均水位和
 *
 */
public class Demo7_Aggregate
{
    public static void main(String[] args) {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env
           .socketTextStream("hadoop103", 8888)
           .map(new MapFunction<String, WaterSensor>()
           {
               @Override
               public WaterSensor map(String value) throws Exception {
                   String[] data = value.split(",");
                   return new WaterSensor(
                       data[0],
                       Long.valueOf(data[1]),
                       Integer.valueOf(data[2])
                   );
               }
           })
           .windowAll(TumblingProcessingTimeWindows.of(Time.seconds(10)))
           // AggregateFunction<IN, ACC, OUT>  :  ACC: 在计算的过程中，要聚合的临时存储的缓冲
           .aggregate(new AggregateFunction<WaterSensor, MyAvg, Double>()
           {
               @Override
               public MyAvg createAccumulator() {
                   return new MyAvg(0, 0d);
               }

               @Override
               public MyAvg add(WaterSensor value, MyAvg accumulator) {
                   accumulator.setCount(accumulator.getCount() + 1);
                   accumulator.setSum(accumulator.getSum() + value.getVc());
                   return accumulator;
               }

               @Override
               public Double getResult(MyAvg accumulator) {
                   return accumulator.sum / accumulator.count;
               }

               //流计算不用实现，批计算需要实现，把 两个 累加器合并
               @Override
               public MyAvg merge(MyAvg a, MyAvg b) {
                   return null;
               }
           }, new ProcessAllWindowFunction<Double, String, TimeWindow>()
           {
               @Override
               public void process(Context context, Iterable<Double> elements, Collector<String> out) throws Exception {
                   out.collect(MyUtil.parseTimeWindow(context.window()) +":" + elements.iterator().next());

               }
           })
           .print();

        try {
                    env.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }

    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class MyAvg{
        private Integer count;
        private Double sum;
    }
}
